<?php

namespace App\Kuafu\Invoice\Validate;

use App\Kuafu\Base\Validate\BaseValidate;
use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\Invoice\Enum\InvoiceCodeEnum;
use Illuminate\Http\Request;

class MoredoValidate extends BaseValidate
{
    public static function moredoInit(Request $request): string
    {
        $rules    = [
            'name'        => 'required',
            'ipWhiteList' => [
                'required',
                function ($attribute, $value, $fail) use ($request) {
                    $ipListStr = $request->ipWhiteList;
                    $ipListArr = explode(',', $ipListStr);
                    foreach ($ipListArr as $value) {
                        $isIp = CommonService::checkIpAddress($value);
                        if (!$isIp) {
                            $fail("ip地址不合法");
                        }
                    }
                }
            ],
            'fpUploadUrl' => 'required',
            'fpCancelUrl' => 'required',
        ];
        $messages = [
            'name.required'        => InvoiceCodeEnum::CODE_TEXT[InvoiceCodeEnum::ERR_NO_NAME],
            'ipWhiteList.required' => InvoiceCodeEnum::CODE_TEXT[InvoiceCodeEnum::ERR_IP_LIST],
            'fpUploadUrl.required' => InvoiceCodeEnum::CODE_TEXT[InvoiceCodeEnum::ERR_UPLOAD_URL],
            'fpCancelUrl.integer'  => InvoiceCodeEnum::CODE_TEXT[InvoiceCodeEnum::ERR_CANCEL_URL],
        ];

        return self::validateCheck($request->all(), $rules, $messages);
    }
}
